/// 
/// MaDAson - Toolkit for Model Driven Software Development
/// 
/// Copyright [2006] Marcus Lankenau
/// 
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///  
///     http://www.apache.org/licenses/LICENSE-2.0
///  
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// 

using System;
using System.Configuration;
using System.IO;
using Madason.MetaModel;
using Madason.MetaModel.DAO;
using Madason.MetaModel.Interfaces;
using System.Xml;
using System.Reflection;
using Madason.MetaModel.Validation;
using Madason.GenUtils;

/// <summary>
/// Summary description for Program
/// </summary>
public class Program
{
	protected const string TEST_PATH = "tests";
	private Madason.MetaModel.Model model;


	/// <summary>
	/// the current model
	/// </summary>
	public Madason.MetaModel.Model Model
	{
		get { return model; }
		set { model = value; }
	}


	/// <summary>
	/// generate ddl script
	/// </summary>
	/// <param name="model"></param>
	/// <param name="argMap"></param>
	public static void GenerateDdl(Madason.MetaModel.Model model, System.Collections.Generic.Dictionary<string, string> argMap)
	{
		using (SensorWriter writer = new SensorWriter("ddl.sql"))
		{
			DDL ddl = new DDL();
			ddl.Model = model;
			//ddl.DBMapper = new DBMapperSqlserver();
			ddl.Render(writer);
		}
	}

	/// <summary>
	/// generate data access objects
	/// </summary>
	/// <param name="classes"></param>
	/// <param name="argMap"></param>
	public static void GenerateDao(Madason.MetaModel.ClassList classes, System.Collections.Generic.Dictionary<string, string> argMap)
	{
		string daoPath = argMap["daopath"];
		string dtoPath = argMap["dtopath"];

		
		foreach (Class cls in classes)
		{
			Console.WriteLine("processing " + cls.ExplicitName);
			if (cls.Stereotype == Stereotype.Enum)
			{
				Enumeration enumeration = new Enumeration();
				string path = cls.Namespace.CreateDirectory(dtoPath).FullName + "\\" + cls.Name + ".cs";
				using (SensorWriter writer = new SensorWriter(path))
				{
					enumeration.Class = cls;
					enumeration.Render(writer);
				}
			}
			if (cls.Stereotype == Stereotype.Entity ||
				cls.Stereotype == Stereotype.Boundary)
			{
				Console.WriteLine("  generating transfer object");
				TransferObject transferObject = new TransferObject();
				string path = cls.Namespace.CreateDirectory(dtoPath).FullName + "\\" + cls.Name + ".cs";
				using (SensorWriter writer = new SensorWriter(path))
				{
					transferObject.Class = cls as Entity;
					transferObject.Render(writer);
				}

				if (cls.Stereotype == Stereotype.Entity)
				{

					Console.WriteLine("  generating abstract dao");
					DaoAbstract daoAbstract = new DaoAbstract();
					path = cls.Namespace.CreateDirectory(daoPath).FullName + "\\" + cls.Name + "Access.cs";
					using (SensorWriter writer = new SensorWriter(path))
					{
						daoAbstract.Class = cls as Entity;
						daoAbstract.Render(writer);
					}

					Console.WriteLine("  generating dao impl");
					DaoImpl daoImpl = new DaoImpl();
					DirectoryInfo createdir = new DirectoryInfo(cls.Namespace.CreateDirectory(daoPath).FullName + "\\SqlServer");
					createdir.Create();
					path = createdir.FullName + "\\" + cls.Name + "AccessSqlServer.cs";
					using (SensorWriter writer = new SensorWriter(path))
					{
						daoImpl.Class = cls as Entity;
						daoImpl.Render(writer);
					}

					if (argMap.ContainsKey(TEST_PATH))
					{
						Console.WriteLine("  generating test");
						DaoTest daoTest = new DaoTest();
						createdir.Create();
						path = cls.Namespace.CreateDirectory(argMap[TEST_PATH]).FullName + "\\Test" + cls.Name + ".cs";
						using (SensorWriter writer = new SensorWriter(path))
						{
							daoTest.Class = cls as Entity;
							daoTest.Render(writer);
						}
					}
				}
			}
		}
	}

	/// <summary>
	/// main function
	/// - parse command argumends
	/// - invoke special generator-functions
	/// </summary>
	/// <param name="args"></param>
	public static int Main(string[] args)
	{
		GeneratorWorkflow workflow = new GeneratorWorkflow(args);

		workflow.AddParameterDefinition("daopath", ParameterType.Directory, "target directory for dao objects", true);
		workflow.AddParameterDefinition("dtopath", ParameterType.Directory, "target directory for dto objects", true);


		workflow.CheckCommandLine();
		workflow.LoadModel();
		workflow.Transform();
		workflow.Validate();

		Model model = workflow.Model;

		//
		// Generate sources
		//
		ClassList classesToGenerate = model.DeepGetClasses().Filter(Stereotype.Entity);
		classesToGenerate.AddRange(model.DeepGetClasses().Filter(Stereotype.Enum));
		classesToGenerate.AddRange(model.DeepGetClasses().Filter(Stereotype.Boundary));
		GenerateDao(classesToGenerate, workflow.Arguments);

		Console.WriteLine("gen ddl");
		GenerateDdl(model, workflow.Arguments);
		return 0;
	}
}
